﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GBMS.APP;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using System.Configuration;
using System.Data;
using Telerik.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using KPIS.GERP.GBMS.MODEL;
using System.Collections;


namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Approve_Project : System.Web.UI.Page
    {
        private ComboBLL comboBLL = new ComboBLL();
        private IProject projectBLL;
        private IProjectHeader pjHeaderBLL;

        protected void Page_Load(object sender, EventArgs e)
        {
            string type = Request.QueryString["type"].ToString();

            if (type.Length > 0)
            {
                hdType.Value = type;
                projectBLL = Project_Factory.GetProjectFactory(type);
                pjHeaderBLL = Project_Factory.GetProjectHeaderFactory(type);
            }

            if (!Page.IsPostBack)
            {
                LoadYear();
            }
        }

        private void LoadYear()
        {
            int CurrYear = SystemInfo._CurrentYearReq;
            ddlYear.Items.Clear();

            for (int i = CurrYear + 5; i >= CurrYear - 5; i--)
            {
                ddlYear.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
            }
            ddlYear.SelectedValue = CurrYear.ToString();
        }
        protected void ddlYear_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            ClearList();
            ddlPrjHeader.DataSource = comboBLL.ListApprovalHeader(ddlYear.SelectedValue);
            ddlPrjHeader.DataBind();
            ddlPrjHeader.Text = ddlPrjHeader.EmptyMessage;
        }
        protected void ddlPrjHeader_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            ClearList();
            LoadData();
        }

        private void LoadData()
        {
            LoadListBoxApproval(ddlPrjHeader.SelectedValue);
        }

        private void LoadListBoxApproval(string seq)
        {
            DataTable info = pjHeaderBLL.ListApproval(seq);
            info.Columns.Add(new DataColumn("ValueTmp", typeof(string)));
            foreach (DataRow dr in info.Rows)
            {
                dr["ValueTmp"] = dr["bgm_app_seq"].ToString() + "-" + dr["bgm_req_seq"].ToString();
            }
            lstApp.DataValueField = "ValueTmp";
            lstApp.DataTextField = "Text";
            lstApp.DataSource = info;
            lstApp.DataBind();
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            //if (e.Argument == "refresh")
            //{
            //    LoadData();
            //}
        }

        protected void btnSelect_Cleck(object sender, EventArgs e)
        {
            for (int i = lstReq.Items.Count - 1; i >= 0; i--)
            {
                if (lstReq.Items[i].Selected)
                {
                    lstApp.Items.Add(new ListItem(lstReq.Items[i].Text, lstReq.Items[i].Value));
                    lstReq.Items.Remove(lstReq.Items[i]);
                }
            }
            chkRePrj();
        }

        protected void btnSelectAll_Cleck(object sender, EventArgs e)
        {
            for (int i = lstReq.Items.Count - 1; i >= 0; i--)
            {
                lstApp.Items.Add(new ListItem(lstReq.Items[i].Text, lstReq.Items[i].Value));
                lstReq.Items.Remove(lstReq.Items[i]);
            }
            chkRePrj();
        }

        protected void btnRemove_Cleck(object sender, EventArgs e)
        {
            for (int i = lstApp.Items.Count - 1; i >= 0; i--)
            {
                if (lstApp.Items[i].Selected)
                {
                    lstReq.Items.Add(new ListItem(lstApp.Items[i].Text, lstApp.Items[i].Value));
                    lstApp.Items.Remove(lstApp.Items[i]);
                }
            }
            chkRePrj();
        }

        protected void btnRemoveAll_Cleck(object sender, EventArgs e)
        {
            for (int i = lstApp.Items.Count - 1; i >= 0; i--)
            {
                lstReq.Items.Add(new ListItem(lstApp.Items[i].Text, lstApp.Items[i].Value));
                lstApp.Items.Remove(lstApp.Items[i]);
            }
            chkRePrj();
        }
        private void chkRePrj()
        {
            btnSave.Enabled = lstReq.Items.Count > 0;
        }
        private void ClearList()
        {
            lstReq.Items.Clear();
            lstApp.Items.Clear(); 
            btnSave.Enabled = false;
        }
        Request_BLL reqBLL = new Request_BLL();
        protected void btnSave_Cleck(object sender, EventArgs e)
        {
            try
            {
                for (int i = lstReq.Items.Count - 1; i >= 0; i--)
                {
                    string[] strValue = lstReq.Items[i].Value.Split('-');
                    projectBLL.ApproveProject(strValue[0], SystemInfo._RejectStatus);
                    reqBLL.ApproveProject(strValue[1], SystemInfo._RejectStatus);
                }
                ClearList();
                RadAjaxManager1.Alert("บันทึกข้อมูลเรียบร้อย");
            }
            catch(Exception ex)
            {
                RadAjaxManager1.Alert(ex.Message);
            }
        }
        protected void btnCancel_Cleck(object sender, EventArgs e)
        {
            ddlPrjHeader.Items.Clear();
            ddlPrjHeader.Text = ddlPrjHeader.EmptyMessage;
            ddlYear.SelectedValue = SystemInfo._CurrentYearReq.ToString();
            ddlYear_SelectedIndexChanged(null, null);
            //ClearList();
        }
    }
}
